package com.jeecms.wicool.action.car;

import static com.jeecms.common.page.SimplePage.cpn;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.cms.manager.main.CmsLogMng;
import com.jeecms.cms.web.CmsUtils;
import com.jeecms.cms.web.WebErrors;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.car.CarBrand;
import com.jeecms.wicool.entity.car.CarSeries;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.manager.car.CarBrandMng;
import com.jeecms.wicool.manager.car.CarSeriesMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;

@Controller
public class CarSeriesAct {
	private static final Logger log = LoggerFactory.getLogger(CarSeriesAct.class);

	@RequestMapping("/carSeries/v_list.do")
	public String list(String seriesName,String shortName,Integer carBrandId ,Integer pageNo,HttpServletRequest request, ModelMap model) {
		Pagination pagination = manager.getPage(seriesName,shortName,carBrandId,cpn(pageNo),
				CookieUtils.getPageSize(request),CmsUtils.getUser(request));
		List<CarBrand> listCarBrand=carBrandMng.getAll(CmsUtils.getUser(request));
		model.addAttribute("pagination", pagination);
		model.addAttribute("listCarBrand",listCarBrand);
		model.addAttribute("seriesName",seriesName);
		model.addAttribute("shortName",shortName);
		model.addAttribute("carBrandId",carBrandId);
		return "car/list_carSeries";
	}
	
	@RequestMapping("/carSeries/v_edit.do")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		CarSeries carSeries = manager.findById(id);
		List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		List<CarBrand> listCarBrand=carBrandMng.getAll(CmsUtils.getUser(request));
		model.addAttribute("carSeries",carSeries);
		model.addAttribute("listShop",listShop);
		model.addAttribute("listCarBrand",listCarBrand);
		return "car/edit_carSeries";
	}
	@RequestMapping("/carSeries/v_view.do")
	public String view(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		CarSeries carSeries = manager.findById(id);
		model.addAttribute("carSeries",carSeries);
		return "car/view_carSeries";
	}
	@RequestMapping("/carSeries/o_save.do")
	public String save(CarSeries bean, Integer shopId,Integer carBrandId,HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(bean, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = manager.save(bean,shopId,carBrandId);
		log.info("save CarSeries id={}", bean.getId());
		cmsLogMng.operating(request, "CarSeries.log.save",  "id=" + bean.getId());
		return "redirect:v_list.do";
	}
	@RequestMapping("/carSeries/o_update.do")
	public String update(CarSeries bean,Integer shopId,Integer carBrandId,Integer pageNo,HttpServletRequest request,ModelMap model) {
		WebErrors errors = validateUpdate(bean.getId(), request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		SimpleDateFormat simpleTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateTime = simpleTime.format(new Date());
		Timestamp t = Timestamp.valueOf(dateTime);

		bean.setUpdateTime(t);
		bean=manager.update(bean,shopId,carBrandId);
		model.addAttribute("message", "global.success");
		log.info("update CarSeries success. id={}", bean.getId());
		cmsLogMng.operating(request, "CarSeries.log.update",  "id=" + bean.getId());
		return list(null,null,null,pageNo,request, model);
	}
	
	@RequestMapping("/carSeries/v_add.do")
	public String add(ModelMap model,HttpServletRequest request) {
		CarSeries carSeries = new CarSeries();
		List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		List<CarBrand> listCarBrand=carBrandMng.getAll(CmsUtils.getUser(request));
		model.addAttribute("carSeries", carSeries);
		model.addAttribute("listShop", listShop);
		model.addAttribute("listCarBrand", listCarBrand);
		return "car/add_carSeries";
	}
	

	@RequestMapping("/carSeries/o_delete.do")
	public String delete(Integer[] ids, String seriesName,String shortName,Integer carBrandId,Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		WebErrors errors = validateDelete(ids, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		CarSeries[] beans = manager.deleteByIds(ids);
		for (CarSeries bean : beans) {
			log.info("delete CarSeries id={}", bean.getId());
			cmsLogMng.operating(request, "CarSeries.log.delete", "id="
					+ bean.getId() + ";name=" + bean.getShortName());
		}
		return list(seriesName,shortName,carBrandId,pageNo, request, model);
	}
	private WebErrors validateSave(CarSeries bean, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		return errors;
	}

	private WebErrors validateEdit(Integer id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		return errors;
	}

	private WebErrors validateUpdate(Integer id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		// TODO 验证是否为管理员，管理员不允许修改。
		return errors;
	}
	private WebErrors validateDelete(Integer[] ids, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		errors.ifEmpty(ids, "ids");
		for (Integer id : ids) {
			vldExist(id, errors);
		}
		return errors;
	}

	private boolean vldExist(Integer id, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		CarSeries entity = manager.findById(id);
		if (errors.ifNotExist(entity, CarSeries.class, id)) {
			return true;
		}
		return false;
	}
	@Autowired
	private CarSeriesMng manager;
	@Autowired
	private CarBrandMng carBrandMng;
	@Autowired
	private WiShopInfoMng shopMng;
	@Autowired
	private CmsLogMng cmsLogMng;
}
